<?php
namespace Twoir\Service\Member;

use Twoir\Service\BaseMember;
use Twoir\System\Model\EnewsMember;

/**
 * 会员登陆
 *
 * 2024.05.05 cighsen02 3651872575
*/
class Login extends BaseMember
{
	// 允许进入字段
	const ALLOW = ['username','password','email','phone','lifetime','key'];
	// 验证码key名称
	const LONGIN_KEY_NAME = 'checkloginkey';
	/**
	 * 邮箱注册
	 *
	 * @param array $add
	 * @return array|throw
	*/
	public function email($add)
	{
		// 参数过滤
		$add = $this->paramFilter($add);

		return [];
	}

	/**
	 * 用户名注册
	 *
	 * @param array $add
	 * @return array|throw
	*/
	public function userName($add)
	{
        global $public_r;

		// 参数过滤
		$add = $this->paramFilter($add);

		$username	= trim($add['username']);
		$password	= trim($add['password']);

		if(!$username || !$password) throw new \Exception('用户名和密码不能为空', 9200);
		//验证码
		if($public_r['loginkey_ok']){

			if (!isset($add['key']) || empty($add['key'])) throw new \Exception('验证码不能为空', 9200);

			$this->checkShowKey(static::LONGIN_KEY_NAME, $add['key'], 1);
		}

		$username	= RepPostVar($username);
		$password	= RepPostVar($password);

		if (empty($user = EnewsMember::getOne(['username' => $username], ['userid','password','groupid','checked','salt'])) || !$this->doCkMemberPw($password, $user['password'], $user['salt'])) {

			throw new \Exception('密码错误', 9200);
		}

		if(0 == $user['checked']){

			if(1 == $public_r['regacttype']) throw new \Exception('用户还未激活，请通过邮件激活', 9200);

			throw new \Exception('用户还未审核，请耐心等待', 9200);
		}

		//默认会员组
		if(empty($r['groupid'])) $user['groupid'] = eReturnMemberDefGroupid();

		$user['groupid'] = (int)$user['groupid'];

		$rnd = make_password(20);//取得随机密码
		// 执行更新
		EnewsMember::update(['userid' => $user['userid']], ['rnd' => $rnd, 'groupid' => $user['groupid'], 'lasttime' => $this->nowtime, 'lastip' => $this->ip, 'loginnum' => 'loginnum + 1', 'lastipport' => $this->port]);

		//设置cookie
		$lifetime 	= isset($add['lifetime']) && $add['lifetime'] ? (int)$add['lifetime'] : 0;
		$logincookie= 0;
		if($lifetime) $logincookie = $this->nowtime + $lifetime;

		$set1 = esetcookie('mlusername', $username, $logincookie);
		$set2 = esetcookie('mluserid', $user['userid'], $logincookie);
		$set3 = esetcookie('mlgroupid', $user['groupid'], $logincookie);
		$set4 = esetcookie('mlrnd', $rnd, $logincookie);
		$this->qGetLoginAuthstr($user['userid'], $username, $rnd, $user['groupid'], $logincookie);
		//清空验证码
		$this->emptyShowKey(static::LONGIN_KEY_NAME);
		$set6 = esetcookie('returnurl', '');

		// 移除
		unset($user['password'], $user['salt']);

		return $user + [
			'username' => $username
		];
	}

	/**
	 * 手机注册，在做之前请在主表增加phone字段
	 *
	 * @param array $add
	 * @return array|throw
	*/
	public function phone($add)
	{
		// 参数过滤
		$add = $this->paramFilter($add);

		return [];
	}

	/**
	 * 验证密码
	 *
	 * @param string $newPassWord
	 * @param string $oldPassWord
	 * @param string $salt
	 * @return bool
	*/
	protected function doCkMemberPw($newPassWord, $oldPassWord, $salt)
	{
		global $ecms_config;

		if(0 == $ecms_config['member']['pwtype']){//单重md5

			if('dg' . md5($newPassWord) === 'dg' . $oldPassWord) return true;
		}
		elseif(1 == $ecms_config['member']['pwtype']){//明码

			if('dg' . $newPassWord === 'dg' . $oldPassWord) return true;
		}
		elseif(3 == $ecms_config['member']['pwtype']){//16位md5

			if('dg'. substr(md5($newPassWord), 8, 16) === 'dg' . $oldPassWord) return true;
		}
		else{//双重md5

			if('dg' . md5(md5($newPassWord) . $salt) === 'dg' . $oldPassWord) return true;
		}

		return false;
	}
}
